/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZDataSource;
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZFolder.View;
/*     */ import com.zimbra.client.ZImapDataSource;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.soap.type.DataSource.ConnectionType;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestImapOneWayImport
/*     */   extends TestCase
/*     */ {
/*     */   private static final String REMOTE_USER_NAME = "testimapimportremote";
/*     */   private static final String LOCAL_USER_NAME = "testimapimportlocal";
/*     */   private static final String NAME_PREFIX = "TestImapOneWayImport";
/*     */   private static final String DS_FOLDER_ROOT = "/TestImapOneWayImport";
/*     */   private static final String REMOTE_PATH_F1 = "/TestImapOneWayImport-f1";
/*     */   private static final String REMOTE_PATH_F2 = "/TestImapOneWayImport-f1/TestImapOneWayImport-f2";
/*     */   private static final String REMOTE_PATH_F3 = "/TestImapOneWayImport-f3";
/*     */   private static final String REMOTE_PATH_F4 = "/TestImapOneWayImport-f3/TestImapOneWayImport-f4";
/*     */   private static final String LOCAL_PATH_F1 = "/TestImapOneWayImport/TestImapOneWayImport-f1";
/*     */   private static final String LOCAL_PATH_F2 = "/TestImapOneWayImport/TestImapOneWayImport-f1/TestImapOneWayImport-f2";
/*     */   private static final String LOCAL_PATH_F3 = "/TestImapOneWayImport/TestImapOneWayImport-f3";
/*     */   private static final String LOCAL_PATH_F4 = "/TestImapOneWayImport/TestImapOneWayImport-f3/TestImapOneWayImport-f4";
/*     */   private static final String LOCAL_PATH_INBOX = "/TestImapOneWayImport/INBOX";
/*     */   private static final String LOCAL_PATH_TRASH = "/TestImapOneWayImport/Trash";
/*     */   private ZMailbox mRemoteMbox;
/*     */   private ZMailbox mLocalMbox;
/*     */   private String mOriginalCleartextValue;
/*     */   private ZDataSource mDataSource;
/*     */   private boolean mOriginalEnableStarttls;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  67 */     cleanUp();
/*     */     
/*     */ 
/*  70 */     if (!TestUtil.accountExists("testimapimportlocal")) {
/*  71 */       TestUtil.createAccount("testimapimportlocal");
/*     */     }
/*  73 */     if (!TestUtil.accountExists("testimapimportremote")) {
/*  74 */       TestUtil.createAccount("testimapimportremote");
/*     */     }
/*  76 */     this.mRemoteMbox = TestUtil.getZMailbox("testimapimportremote");
/*  77 */     this.mLocalMbox = TestUtil.getZMailbox("testimapimportlocal");
/*     */     
/*     */ 
/*  80 */     ZFolder folder = this.mLocalMbox.getFolderByPath("/TestImapOneWayImport");
/*  81 */     if (folder == null) {
/*  82 */       folder = TestUtil.createFolder(this.mLocalMbox, "TestImapOneWayImport");
/*     */     }
/*     */     
/*     */ 
/*  86 */     int port = Integer.parseInt(TestUtil.getServerAttr("zimbraImapBindPort"));
/*  87 */     this.mDataSource = new ZImapDataSource("TestImapOneWayImport", true, "localhost", port, "testimapimportremote", "test123", folder.getId(), DataSource.ConnectionType.cleartext, true);
/*     */     
/*     */ 
/*  90 */     String id = this.mLocalMbox.createDataSource(this.mDataSource);
/*  91 */     this.mDataSource = null;
/*  92 */     for (ZDataSource ds : this.mLocalMbox.getAllDataSources()) {
/*  93 */       if (ds.getId().equals(id)) {
/*  94 */         this.mDataSource = ds;
/*     */       }
/*     */     }
/*  97 */     assertNotNull(this.mDataSource);
/*     */     
/*  99 */     this.mOriginalCleartextValue = TestUtil.getServerAttr("zimbraImapCleartextLoginEnabled");
/* 100 */     TestUtil.setServerAttr("zimbraImapCleartextLoginEnabled", "TRUE");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 105 */     this.mOriginalEnableStarttls = LC.javamail_imap_enable_starttls.booleanValue();
/* 106 */     LC.javamail_imap_enable_starttls.setDefault(Boolean.toString(false));
/*     */   }
/*     */   
/*     */ 
/*     */   public void testImapOneWayImport()
/*     */     throws Exception
/*     */   {
/* 113 */     ZimbraLog.test.info("Testing adding message to remote inbox.");
/* 114 */     String remoteQuery = "in:inbox msg1";
/* 115 */     TestUtil.addMessage(this.mRemoteMbox, "TestImapOneWayImport msg1", Integer.toString(2), "u");
/* 116 */     checkMsgCount(this.mRemoteMbox, remoteQuery, 1);
/* 117 */     assertNull(this.mLocalMbox.getFolderByPath("/TestImapOneWayImport/INBOX"));
/* 118 */     List<ZMessage> msgs = TestUtil.search(this.mRemoteMbox, remoteQuery);
/* 119 */     assertEquals("Message count in remote inbox", 1, msgs.size());
/* 120 */     ZMessage msg = (ZMessage)msgs.get(0);
/* 121 */     assertTrue("Remote message is read", msg.isUnread());
/*     */     
/* 123 */     importImap();
/*     */     
/* 125 */     String localInboxQuery = "in:/TestImapOneWayImport/INBOX";
/* 126 */     checkMsgCount(this.mLocalMbox, localInboxQuery, 1);
/* 127 */     msgs = TestUtil.search(this.mRemoteMbox, remoteQuery);
/* 128 */     msg = (ZMessage)msgs.get(0);
/* 129 */     assertTrue("Remote message is read", msg.isUnread());
/* 130 */     compare();
/*     */     
/*     */ 
/*     */ 
/* 134 */     ZimbraLog.test.info("Testing flag.");
/* 135 */     msgs = TestUtil.search(this.mRemoteMbox, remoteQuery);
/* 136 */     assertEquals("Message count in remote inbox", 1, msgs.size());
/* 137 */     msg = (ZMessage)msgs.get(0);
/* 138 */     assertTrue("Remote message is read", msg.isUnread());
/* 139 */     String remoteId = msg.getId();
/* 140 */     this.mRemoteMbox.flagMessage(remoteId, true);
/*     */     
/*     */ 
/* 143 */     msgs = TestUtil.search(this.mLocalMbox, localInboxQuery);
/* 144 */     assertEquals("Message count in local inbox", 1, msgs.size());
/* 145 */     msg = (ZMessage)msgs.get(0);
/* 146 */     assertFalse("Local message is flagged", msg.isFlagged());
/* 147 */     assertTrue("Local message is read", msg.isUnread());
/*     */     
/* 149 */     importImap();
/*     */     
/*     */ 
/* 152 */     msgs = TestUtil.search(this.mLocalMbox, localInboxQuery);
/* 153 */     assertEquals("Message count in local inbox", 1, msgs.size());
/* 154 */     msg = (ZMessage)msgs.get(0);
/* 155 */     assertTrue("Local message is flagged", msg.isFlagged());
/* 156 */     assertTrue("Local message is read", msg.isUnread());
/*     */     
/* 158 */     compare();
/*     */     
/*     */ 
/*     */ 
/* 162 */     ZimbraLog.test.info("Testing remote move to trash.");
/* 163 */     this.mRemoteMbox.trashMessage(remoteId);
/* 164 */     checkMsgCount(this.mRemoteMbox, "in:trash", 1);
/* 165 */     checkMsgCount(this.mLocalMbox, "in:trash", 0);
/* 166 */     importImap();
/* 167 */     checkMsgCount(this.mLocalMbox, "in:/TestImapOneWayImport/Trash", 1);
/* 168 */     compare();
/*     */     
/*     */ 
/*     */ 
/* 172 */     ZimbraLog.test.info("Testing folder creation.");
/* 173 */     TestUtil.createFolder(this.mRemoteMbox, "/TestImapOneWayImport-f1");
/* 174 */     TestUtil.createFolder(this.mRemoteMbox, "/TestImapOneWayImport-f1/TestImapOneWayImport-f2");
/* 175 */     TestUtil.createFolder(this.mLocalMbox, "/TestImapOneWayImport/TestImapOneWayImport-f3");
/* 176 */     TestUtil.createFolder(this.mLocalMbox, "/TestImapOneWayImport/TestImapOneWayImport-f3/TestImapOneWayImport-f4");
/* 177 */     importImap();
/*     */     
/*     */ 
/* 180 */     assertNotNull("Local folder /TestImapOneWayImport/TestImapOneWayImport-f1", this.mLocalMbox.getFolderByPath("/TestImapOneWayImport/TestImapOneWayImport-f1"));
/* 181 */     assertNotNull("Local folder /TestImapOneWayImport/TestImapOneWayImport-f1/TestImapOneWayImport-f2", this.mLocalMbox.getFolderByPath("/TestImapOneWayImport/TestImapOneWayImport-f1/TestImapOneWayImport-f2"));
/*     */     
/*     */ 
/* 184 */     assertNull("Remote folder /TestImapOneWayImport-f3", this.mRemoteMbox.getFolderByPath("/TestImapOneWayImport-f3"));
/* 185 */     assertNull("Remote folder /TestImapOneWayImport-f3/TestImapOneWayImport-f4", this.mRemoteMbox.getFolderByPath("/TestImapOneWayImport-f3/TestImapOneWayImport-f4"));
/*     */     
/*     */ 
/* 188 */     ZimbraLog.test.info("Testing UIDVALIDITY change.");
/* 189 */     ZFolder localFolder1 = this.mLocalMbox.getFolderByPath("/TestImapOneWayImport/TestImapOneWayImport-f1");
/* 190 */     ZFolder remoteFolder1 = this.mRemoteMbox.getFolderByPath("/TestImapOneWayImport-f1");
/*     */     
/*     */ 
/* 193 */     String subject = "TestImapOneWayImport msg2";
/* 194 */     String originalId = TestUtil.addMessage(this.mLocalMbox, subject, localFolder1.getId());
/* 195 */     msgs = TestUtil.search(this.mLocalMbox, subject);
/* 196 */     assertEquals(1, msgs.size());
/* 197 */     assertEquals(originalId, ((ZMessage)msgs.get(0)).getId());
/*     */     
/*     */ 
/* 200 */     this.mRemoteMbox.renameFolder(remoteFolder1.getId(), "TestImapOneWayImport-renamed");
/* 201 */     this.mRemoteMbox.renameFolder(remoteFolder1.getId(), "TestImapOneWayImport-f1");
/* 202 */     importImap();
/*     */     
/*     */ 
/*     */ 
/* 206 */     msgs = TestUtil.search(this.mLocalMbox, subject);
/* 207 */     assertEquals(1, msgs.size());
/* 208 */     assertFalse("Message id did not change: " + originalId, originalId.equals(((ZMessage)msgs.get(0)).getId()));
/*     */     
/*     */ 
/* 211 */     ZimbraLog.test.info("Testing simultaneous message add and folder delete 1.");
/* 212 */     ZFolder remoteFolder2 = this.mRemoteMbox.getFolderByPath("/TestImapOneWayImport-f1/TestImapOneWayImport-f2");
/* 213 */     TestUtil.addMessage(this.mRemoteMbox, "TestImapOneWayImport msg3", remoteFolder2.getId());
/* 214 */     ZFolder localFolder3 = this.mLocalMbox.getFolderByPath("/TestImapOneWayImport/TestImapOneWayImport-f3");
/* 215 */     TestUtil.createFolder(this.mRemoteMbox, "/TestImapOneWayImport-f3");
/* 216 */     this.mLocalMbox.deleteFolder(localFolder3.getId());
/* 217 */     checkMsgCount(this.mLocalMbox, "in:/TestImapOneWayImport/TestImapOneWayImport-f1/TestImapOneWayImport-f2", 0);
/* 218 */     importImap();
/*     */     
/*     */ 
/* 221 */     assertNotNull("Remote folder 3", this.mRemoteMbox.getFolderByPath("/TestImapOneWayImport-f3"));
/* 222 */     checkMsgCount(this.mLocalMbox, "in:/TestImapOneWayImport/TestImapOneWayImport-f1/TestImapOneWayImport-f2", 1);
/*     */     
/*     */ 
/*     */ 
/* 226 */     ZimbraLog.test.info("Testing simultaneous message add and folder delete 2.");
/* 227 */     ZFolder localFolder = this.mLocalMbox.getFolderByPath("/TestImapOneWayImport/TestImapOneWayImport-f1/TestImapOneWayImport-f2");
/* 228 */     TestUtil.addMessage(this.mLocalMbox, "TestImapOneWayImport msg4", localFolder.getId());
/* 229 */     remoteFolder1 = this.mRemoteMbox.getFolderByPath("/TestImapOneWayImport-f1");
/* 230 */     this.mRemoteMbox.deleteFolder(remoteFolder1.getId());
/*     */     
/* 232 */     assertNotNull("Local folder 1", this.mLocalMbox.getFolderByPath("/TestImapOneWayImport/TestImapOneWayImport-f1"));
/*     */     
/* 234 */     assertNull("Remote folder 1", this.mRemoteMbox.getFolderByPath("/TestImapOneWayImport-f1"));
/* 235 */     importImap();
/*     */     
/*     */ 
/* 238 */     assertNull("Remote folder 1", this.mRemoteMbox.getFolderByPath("/TestImapOneWayImport-f1"));
/*     */     
/* 240 */     assertNull("Local folder 1", this.mLocalMbox.getFolderByPath("/TestImapOneWayImport/TestImapOneWayImport-f1"));
/*     */     
/* 242 */     checkMsgCount(this.mRemoteMbox, "msg4", 0);
/*     */     
/*     */ 
/* 245 */     ZimbraLog.test.info("Testing sync from local to remote.");
/* 246 */     checkMsgCount(this.mLocalMbox, "in:/TestImapOneWayImport/INBOX", 0);
/* 247 */     ZFolder localInbox = this.mLocalMbox.getFolderByPath("/TestImapOneWayImport/INBOX");
/* 248 */     TestUtil.addMessage(this.mLocalMbox, "TestImapOneWayImport msg5", localInbox.getId());
/* 249 */     checkMsgCount(this.mLocalMbox, "in:/TestImapOneWayImport/INBOX", 1);
/* 250 */     checkMsgCount(this.mRemoteMbox, "in:inbox", 0);
/*     */     
/*     */ 
/* 253 */     checkMsgCount(this.mLocalMbox, "in:/TestImapOneWayImport/Trash", 1);
/* 254 */     ZFolder localTrash = this.mLocalMbox.getFolderByPath("/TestImapOneWayImport/Trash");
/* 255 */     this.mLocalMbox.emptyFolder(localTrash.getId());
/* 256 */     checkMsgCount(this.mLocalMbox, "in:/TestImapOneWayImport/Trash", 0);
/* 257 */     checkMsgCount(this.mRemoteMbox, "in:trash", 1);
/* 258 */     importImap();
/*     */     
/*     */ 
/* 261 */     checkMsgCount(this.mRemoteMbox, "in:inbox msg5", 0);
/* 262 */     checkMsgCount(this.mRemoteMbox, "in:trash", 1);
/*     */   }
/*     */   
/*     */   private void checkMsgCount(ZMailbox mbox, String query, int expectedCount) throws Exception
/*     */   {
/* 267 */     List<ZMessage> msgs = TestUtil.search(mbox, query);
/* 268 */     assertEquals("Result size for query '" + query + "'", expectedCount, msgs.size());
/*     */   }
/*     */   
/*     */   private void importImap() throws Exception
/*     */   {
/* 273 */     TestUtil.importDataSource(this.mDataSource, this.mLocalMbox, this.mRemoteMbox);
/*     */   }
/*     */   
/*     */   private void compare()
/*     */     throws Exception
/*     */   {
/* 279 */     ZFolder folder1 = this.mRemoteMbox.getUserRoot();
/* 280 */     ZFolder folder2 = this.mLocalMbox.getFolderByPath("/TestImapOneWayImport");
/* 281 */     compare(this.mRemoteMbox, folder1, this.mLocalMbox, folder2);
/*     */   }
/*     */   
/*     */   private void compare(ZMailbox mbox1, ZFolder folder1, ZMailbox mbox2, ZFolder folder2) throws Exception
/*     */   {
/* 286 */     assertNotNull(mbox1);
/* 287 */     assertNotNull(folder1);
/* 288 */     assertNotNull(mbox2);
/* 289 */     assertNotNull(folder2);
/*     */     
/*     */ 
/* 292 */     for (ZFolder child1 : folder1.getSubFolders()) {
/* 293 */       if (isMailFolder(child1)) {
/* 294 */         ZFolder child2 = folder2.getSubFolderByPath(child1.getName());
/* 295 */         String msg = String.format("Could not find folder %s/%s for %s", new Object[] { folder2.getPath(), child1.getName(), mbox2.getName() });
/*     */         
/* 297 */         assertNotNull(msg, child2);
/* 298 */         compare(mbox1, child1, mbox2, child2);
/*     */       }
/*     */     }
/* 301 */     assertEquals("Message count doesn't match (folder1 = " + folder1 + ", folder2 = " + folder2 + ")", folder1.getMessageCount(), folder2.getMessageCount());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 306 */     if ((!folder1.getPath().equals("/")) && (!folder2.getPath().equals("/"))) {
/* 307 */       List<ZMessage> msgs1 = TestUtil.search(mbox1, "in:" + folder1.getPath());
/* 308 */       List<ZMessage> msgs2 = TestUtil.search(mbox2, "in:" + folder2.getPath());
/* 309 */       compareMessages(msgs1, msgs2);
/*     */     }
/*     */   }
/*     */   
/*     */   private boolean isMailFolder(ZFolder folder) {
/* 314 */     ZFolder.View view = folder.getDefaultView();
/* 315 */     return (view == null) || (view == ZFolder.View.message) || (view == ZFolder.View.conversation);
/*     */   }
/*     */   
/*     */   private void compareMessages(List<ZMessage> msgs1, List<ZMessage> msgs2)
/*     */     throws Exception
/*     */   {
/* 321 */     Map<String, ZMessage> msgMap = new HashMap();
/* 322 */     for (ZMessage msg : msgs1) {
/* 323 */       msgMap.put(msg.getMessageIdHeader(), msg);
/*     */     }
/*     */     
/*     */ 
/* 327 */     for (ZMessage msg2 : msgs2) {
/* 328 */       String id = msg2.getMessageIdHeader();
/* 329 */       ZMessage msg1 = (ZMessage)msgMap.remove(id);
/* 330 */       assertNotNull("Found message '" + msg2.getSubject() + "' in mbox2 but not in mbox1", msg1);
/* 331 */       assertEquals("Message content", msg1.getContent(), msg2.getContent());
/* 332 */       String f1 = msg1.getFlags() != null ? msg1.getFlags() : "";
/* 333 */       String f2 = msg2.getFlags() != null ? msg2.getFlags() : "";
/* 334 */       assertEquals("Flags for message '" + msg1.getSubject() + "' don't match", f1, f2);
/*     */     }
/*     */     
/*     */ 
/* 338 */     if (msgMap.size() != 0) {
/* 339 */       List<String> subjects = new ArrayList();
/* 340 */       for (ZMessage msg : msgMap.values()) {
/* 341 */         subjects.add(msg.getSubject());
/*     */       }
/* 343 */       fail("Found messages in mbox1 but not in mbox2: " + StringUtil.join(",", subjects));
/*     */     }
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 349 */     cleanUp();
/* 350 */     TestUtil.setServerAttr("zimbraImapCleartextLoginEnabled", this.mOriginalCleartextValue);
/*     */     
/* 352 */     LC.javamail_imap_enable_starttls.setDefault(Boolean.toString(this.mOriginalEnableStarttls));
/*     */   }
/*     */   
/*     */   public void cleanUp() throws Exception
/*     */   {
/* 357 */     if (TestUtil.accountExists("testimapimportremote")) {
/* 358 */       TestUtil.deleteAccount("testimapimportremote");
/*     */     }
/* 360 */     if (TestUtil.accountExists("testimapimportlocal")) {
/* 361 */       TestUtil.deleteAccount("testimapimportlocal");
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 367 */     TestUtil.cliSetup();
/* 368 */     TestUtil.runTest(TestImapOneWayImport.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestImapOneWayImport.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */